<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户信息</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row :gutter="20">
        <el-col :span="7">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addDialogVisible=true">添加用户</el-button>
        </el-col>
      </el-row>

    <!-- 这里的是写死的，如果用数据请求看p44 15获取用户列表数据 -->
    <el-table
      :data="tableData"
      style="width: 100%" border stripe>
      <el-table-column
        prop="date"
        label="日期"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="名称"
        width="180">
      </el-table-column>
      <el-table-column
        prop="address"
        label="角色">
      </el-table-column>
      <el-table-column
        prop="min"
        label="邮箱">
      </el-table-column>
      <el-table-column  label="状态">
        <el-switch v-model="value" active-color="#13ce66" inactive-color="#ff4949">
        </el-switch>
      </el-table-column>
      <el-table-column label="操作">
        <!-- <el-button type="primary" icon="el-icon-edit" size="mini" @click="showEditDialog()"></el-button>
        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeUserById()"></el-button>
        <el-tooltip effect="dark" content="分配角色" placement="top-start" :enterable="false">
          <el-button type="warning" icon="el-icon-setting" size="mini" ></el-button>
        </el-tooltip> -->
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage4"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="10"
      layout="total, sizes, prev, pager, next, jumper"
      :total="100">
    </el-pagination><!--实现分页效果看视频 p49 20实现分页效果-->
    </el-card>
    <!--添加对话框-->
    <el-dialog
      title="提示"
      :visible.sync="addDialogVisible"
      width="50%" @close="addDialogClose">
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-input v-model="addForm.role"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
    <!--修改用户对话框-->
    <el-dialog
      title="修改用户"
      :visible.sync="editDialogVisible"
      width="50%">
      <span>这是一段信息</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {getUserss} from '@/api/user'
export default {
  name:'UserInfo',
  data() {

    //自定义邮箱规则
    // var checkEmail = (rule,value,cd)=>{
    //   const redEmail = /^([a-zA-Z0_9_-])+@(
    //     [a-zA-Z0_9_-])+(\.[a-zA-Z0_9_-])+/
    //   if(redEmail.test(value)){
    //     return cd()
    //   } 
    //   cd(new Errer('请输入合法的邮箱'))
    // }
    return {
      value:true,
      tableData: [],
      //控制添加用户对话框的显示与英藏
      addDialogVisible:false,
      //添加用户的表单数据
      addForm:{
        username:'',
        password:'',
        email:'',
        role:''
      },
      //添加表单的验证规则对象
      addFormRules:{
        username:[
          {required: true, message: '请输入用户名字', trigger: 'blur'},
          {min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur'}
        ],
        password:[
          {required: true, message: '请输入用户密码', trigger: 'blur'},
          {min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur'}
        ],
        email:[
          {required: true, message: '请输入用户邮箱', trigger: 'blur'},
        ],
        role:[
          {required: true, message: '请输入用户邮箱', trigger: 'blur'},
        ]
      },
      editDialogVisible:false
    } 
  },
  created(){
    getUserss({}).then((res)=>{
      console.log(res)
    })
  },
  methods:{
    addDialogClose(){
      this.$refs.addFormRef.resetFields()
    },
    showEditDialog(){
      this.editDialogVisible =true
    },
    //这里的删除时要根据Id删除对应的用户信息，这里要用到数据请求的数据
    removeUserById(){
       this.$confirm('是否永久删除该用户, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    }
  }
}
</script>

<style scoped>
  .el-table{
    margin-top: 15px;
  }
  .el-pagination{
    margin-top: 15px;
  }
</style>